﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Swift;
using Swift.Math;

namespace SCM
{
    /// <summary>
    /// 普通 PVP 关卡
    /// </summary>
    public class LvPVP : Level
    {
        public override string LevelID { get { return "PVP"; } }
        public override string DisplayName { get { return "对战地图"; } }
        public override string Description { get { return "用于普通 PVP 对战"; } }
        public override bool IsPVP { get { return true; } }

        public override void InitResource(Room r)
        {
            r.AddResource(1, "Money", 100);
            r.AddResource(2, "Money", 100);
        }

        public override int CheckWinner(Room r)
        {
            var p1 = new Vec2(MapSize.x / 2, 100);
            var p2 = new Vec2(MapSize.x / 2, MapSize.y - 100);
            if (r.GetUnitsInCircleArea(p1, 100, (u) => (u.UnitType == "CommanderCenter"
                || u.cfg.LevelUpFrom == "CommanderCenter") && u.Player == 1).Length == 0)
                return 2;
            if (r.GetUnitsInCircleArea(p2, 100, (u) => (u.UnitType == "CommanderCenter"
                || u.cfg.LevelUpFrom == "CommanderCenter") && u.Player == 2).Length == 0)
                return 1;
            else
                return -1;
        }

        Vec2[] poses = null;
        Fix64 ccOffset = Fix64.Zero;
        public override void InitMapSetting(Room r)
        {
            // 矿点位置
            ccOffset = 50;
            poses = new Vec2[]
            {
                new Vec2(MapHalfSize.x, ccOffset),
                new Vec2(MapHalfSize.x - 200, ccOffset + 150),
                new Vec2(MapHalfSize.x + 200, ccOffset + 300),
            };

            foreach (var p in poses)
            {
                r.AddNewUnit(null, "CCStub", p, 0);
                r.AddNewUnit(null, "CCStub", MapSize - p, 0);
            }
        }

        public override void InitBuilding(Room r)
        {
            // 初始基地和治疗塔
            var cc1 = r.AddNewUnit(null, "CommanderCenter", poses[0], 1);
            cc1.BuildingCompleted = true;
            cc1.Hp = cc1.cfg.MaxHp;
            var ht1 = r.AddNewUnit(null, "HealingTower", poses[0], 1);
            ht1.BuildingCompleted = true;
            ht1.Hp = ht1.cfg.MaxHp;
            var cc2 = r.AddNewUnit(null, "CommanderCenter", MapSize - poses[0], 2);
            cc2.BuildingCompleted = true;
            cc2.Hp = cc2.cfg.MaxHp;
            var ht2 = r.AddNewUnit(null, "HealingTower", MapSize - poses[0], 2);
            ht2.BuildingCompleted = true;
            ht2.Hp = ht2.cfg.MaxHp;

            // 开局临时视野
            r.AddNewUnit(null, "TempVisionAtBeginning", MapHalfSize, 0).Hp = 2;
        }

        public override void InitBattleUnits(Room r)
        {
        }
    }
}
